package gui.item;

import gui.common.*;
import hit.core.HomeInventoryTracker;
import hit.core.ItemFacade;
import hit.core.Item;
import hit.dao.DataAccessObject;
import hit.dao.FactoryReference;

import java.util.Calendar;
import java.util.Date;

/**
 * Controller class for the edit item view.
 */
public class EditItemController extends Controller 
										implements IEditItemController {
	ItemFacade facade;
	Item toEdit;
	String barcode;
	String description;
	Date entryDate;
	/**
	 * Constructor.
	 * 
	 * @param view Reference to edit item view
	 * @param target Item that is being edited
	 */
	public EditItemController(IView view, ItemData target) {
		super(view);

		facade = HomeInventoryTracker.Instance().GetItemFacade();
		toEdit = (Item)(target.getTag());
		entryDate = target.getEntryDate();
		barcode = toEdit.GetBarcode().GetValue();
		description = toEdit.GetProduct().GetDescription();
		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditItemView getView() {
		return (IEditItemView)(super.getView());
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
		getView().enableBarcode(false);
		getView().enableDescription(false);
		getView().enableEntryDate(true);
		boolean result = false;
		try{
			entryDate = getView().getEntryDate();
			
			//the entry date must be in the past
			if (entryDate != null && entryDate.before(new Date()))
				result = true;
			
			//check that the year is after 2000
			if (entryDate != null) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(entryDate);
				int year = calendar.get(Calendar.YEAR);
				
				if (year < 2000)
					result = false;
			}
		}
		catch (IllegalArgumentException e){
			result = false;
		}
		getView().enableOK(result);
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() {
		getView().setBarcode(barcode);
		getView().setDescription(description);
		getView().setEntryDate(entryDate);
		
		enableComponents();
	}

	//
	// IEditItemController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * edit item view is changed by the user.
	 */
	@Override
	public void valuesChanged() {
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit item view.
	 */
	@Override
	public void editItem() {
		try {
			toEdit.SetEntry(entryDate);
			facade.EditItem(toEdit, toEdit);
			
			DataAccessObject dao = FactoryReference.GetFactory().CreateDataAccessObject();
			dao.StartTransaction();
			
			dao.GetItemDAO().Update(toEdit);
			dao.EndTransaction(true);
			dao.CloseConnection();
		}
		catch (Exception e) {
			getView().displayErrorMessage("Unable to edit item.");
		}
	}

}

